package reviewd

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
)

const (
	// DomainID 领域唯一标志
	DomainID = "review"
	// Review 标识
	Review = DomainID
)

// DomainOption 域构建参数
type DomainOption struct {
	ReviewRepo ReviewRepo
}

// NewDomain 支撑子域审核域
func NewDomain(p ddd.Domain, opt DomainOption) ReviewDomain {
	d := &reviewDomain{}
	d.Init(d, p, DomainID)
	initReview(d, opt.ReviewRepo)
	return d
}

// ReviewDomain 审核域
type ReviewDomain interface {
	ddd.Domain
	ReviewAggregateRoot() ReviewAggregateRoot
}
type reviewDomain struct {
	ddd.BaseDomain
	reviewAggregateRoot ReviewAggregateRoot
}

func (i *reviewDomain) ReviewAggregateRoot() ReviewAggregateRoot {
	if i.reviewAggregateRoot != nil {
		return i.reviewAggregateRoot
	}
	r := i.BaseDomain.AggregateRootByID(Review)
	if r == nil {
		panic("没有初始化ReviewAggregateRoot")
	}
	ir, ok := r.(ReviewAggregateRoot)
	if !ok {
		panic("初始化ReviewAggregateRoot，类型错误")
	}
	i.reviewAggregateRoot = ir
	return ir

}
